const express = require("express");
const multer = require("multer");
const path = require("path");
const cors = require("cors");
const app = express(cors());
const PORT = process.env.PORT || 3001;
// 设置模板引擎为EJS
app.set("view engine", "ejs");

// 设置模板文件的存放目录（可选，默认为'views'）
app.set("views", "./views");
// 设置存储配置
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads/"); // 文件保存路径
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname)); // 文件名，这里使用时间戳和文件扩展名
  },
});

// 创建multer实例
const upload = multer({ storage: storage });
// 对于 Express 4.16.0 及以上版本，使用内置的中间件  
app.use(express.json()); // 解析 application/json  
app.use(express.urlencoded({ extended: true })); // 解析 application/x-www-form-urlencoded  
 
// 设置静态文件路径
app.use(express.static(path.join(__dirname, "public")));
// 根路径重定向到index.html
app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "index.html"));
});
// 根路径重定向到index.html
app.get("/delete-pages", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "index.html"));
});

app.post("/delete-pages", upload.array("files[]", 12), (req, res) => {
  // req.files 是 `multer` 放置上传文件的数组
  // req.body 将包含表单中的其他字段（如果有的话）
  console.log(req.files); // 打印上传的文件信息
  // res.send("Files uploaded successfully!");
  const data = {
    message: "Files uploaded successfully!",
    success: true,
    data: {
      files: req.files,
    },
  };
  res.header("Access-Control-Allow-Origin", "*");  
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  
 
  res.json(data);
});

app.post("/pdfinfo",(req,res)=>{
  console.log(req.body)
  const data = {
    message: "Files uploaded successfully!",
    success: true,
    data: {
      files: [{
        name: "1.pdf"
      },{
        name: "2.pdf"
      }],
    },
  };
  res.header("Access-Control-Allow-Origin", "*");  
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  
  res.json(data);
})


app.get("/merge", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "merge.html"));
});
app.get("/split", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "split.html"));
});
app.get("/resize", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "resize.html"));
});
app.get("/convert", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "convert.html"));
});

app.get("/crop", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "crop.html"));
});

app.get("/rotate", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "rotate.html"));
});

app.get("/optimize", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "optimize.html"));
});

app.get("/reorder-pages", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "reorder-pages.html"));
});

app.get("/multipage", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "multipage.html"));
});

app.get("/jpg-to-pdf", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "jpg-to-pdf.html"));
});
// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
